package com.hxht.storage.server.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Repository

public class BaseDaoImpl<T>{
	
	@PersistenceContext(unitName="nvmpentityManagerFactory")
	EntityManager entityManage;
	
	protected Class<T> clazz;
	
	public List<T> findAll() {
		TypedQuery<T> query = entityManage.createQuery("from "+ clazz.getSimpleName(), clazz);
		
		return query.getResultList();
	}
	
	public void insert(T t){
		entityManage.persist(t);
	}
	
	public void update(T t){
		entityManage.merge(t);
	}
	
	public void remove(T t){
		entityManage.remove(t);
	}
	
	public void removeById(Object id){
		entityManage.remove(findById(id));
	}
	
	public T findById(Object id){
		return entityManage.find(clazz, id);
	}
}
